home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / SciAn / src / ScianFontMapper.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  13KB  |  606 lines

  1. /*ScianFontMapper.c
  2.   Eric Pepke
  3.   Asks user to map special characters in the SGI fonts
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <gl.h>
  8. #include <device.h>
  9. #include "machine.h"
  10. #ifdef FONTS4D
  11. #include <fmclient.h>
  12. #endif
  13.  
  14.  
  15. #define BLOCKSIZE    40
  16. #define TOPSPACE    200
  17. #define INSTRUCTIONX    (10)
  18. #define INSTRUCTIONY    (9 * BLOCKSIZE + TOPSPACE - 20)
  19. #define INSTRUCTIONLINE    (18)
  20. #define SAMPLEX        (16 * BLOCKSIZE - 60)
  21. #define SAMPLEY        (9 * BLOCKSIZE + TOPSPACE - 60)
  22. #define SAMPLERADIUS    20
  23.  
  24. #define LINEWIDTH    2
  25. #define NOTFOUNDSIZE    4
  26.  
  27. #ifdef FONTS4D
  28. #define FONTSIZE    24
  29. #else
  30. #define FONTSIZE    12
  31. #endif
  32.  
  33.  
  34. #define CHAR_COPYRIGHT    0    /*Copyright character*/
  35. #define CHAR_REGISTERED    1    /*Registered copyright*/
  36. #define CHAR_CROSS    2    /*Cross product*/
  37. #define CHAR_DOT    3    /*Dot product*/
  38. #define N_CHARS        4    /*Number of chars to assign*/
  39.  
  40. int charAssignments[N_CHARS];
  41.  
  42. char *charNames[N_CHARS] =
  43.     {
  44.     "COPYRIGHT    ",
  45.     "REGISTERED   ",
  46.     "CROSS        ",
  47.     "DOT          "
  48.     };
  49.  
  50. #define ST_BEGIN    0    /*Begin state*/
  51. #define ST_INSTR2    1    /*Second instruction state*/
  52. #define ST_COPYRIGHT    2    /*Get copyright*/
  53. #define ST_REGISTERED    3    /*Get registered*/
  54. #define ST_CROSS    4    /*Get cross*/
  55. #define ST_DOT        5    /*Get dot*/
  56. #define ST_THANKYOU    6    /*Thanks and close*/
  57. #define N_STATES    7    /*# of states*/
  58.  
  59. int running = 1;    /*True iff running*/
  60. int state = ST_BEGIN;        /*State of the system*/
  61.  
  62. char tempStr[400];
  63.  
  64. char *instructions[N_STATES] =
  65.     {
  66. /*                                                                   | */
  67. "SciAn uses characters in the Silicon Graphics font manager which are\n\
  68. outside the normal range of ASCII characters.  Unfortunately, which \n\
  69. character is which tends to change depending on the version of the \n\
  70. font manager.  Your help is required to find and asign characters.\n\n\
  71. Click with the mouse anywhere in the window to continue, or press \n\
  72. the Esc key to abort.",
  73.  
  74. "You will be presented with a series of characters, described in a\n\
  75. short paragraph with an accompanying approximate illustration.  For \n\
  76. each character, please find the character it most resembles in the \n\
  77. boxes below and click in the appropriate box.  If you cannot find \n\
  78. the character, click in the Not Shown box.\n\n\
  79. If you make a mistake, press the Esc key to go back.\n\n\
  80. Click with the mouse anywhere in the window to continue.",
  81.  
  82. "Please find and click on the copyright\n\
  83. symbol, which looks like a little 'C' in\n\
  84. a circle and resembles the illustration\n\
  85. on the right.  If the symbol is not shown,\n\
  86. click in the Not Shown box.",
  87.  
  88. "Please find and click on the registered\n\
  89. copyright symbol, which looks like a \n\
  90. little 'R' in a circle and resembles the\n\
  91. illustration on the right.  If the symbol\n\
  92. is not shown, click in the Not Shown box.",
  93.  
  94. "Pleas find and click on the cross\n\
  95. product symbol, which looks like a big\n\
  96. 'X' centered in the character and resembles\n\
  97. the illustration on the right.  If the\n\
  98. symbol is not shown, click in the Not\n\
  99. Shown box.",
  100.  
  101. "Pleas find and click on the dot\n\
  102. product symbol, which looks like a dot\n\
  103. centered in the character space and \n\
  104. resembles the illustration on the right.\n\
  105. This one can be hard to find, so look\n\
  106. carefully.  If the symbol is not shown,\n\
  107. click in the Not Shown box.",
  108.  
  109. "Thank you for filling in the font information for SciAn.  Please \n\
  110. click anywhere in the window to write the configuration file and \n\
  111. exit.  Then proceed to the next step in the installation process."
  112.     };
  113.  
  114. #if 0
  115. TestBlock(int l, int r, int b, int t, int foreColor, int backColor)
  116. /*Does a test block*/
  117. {
  118.     int lt, rt, x, y, fx, fy;
  119.  
  120.     color(backColor);
  121.     rectf(l, b, r, t);
  122.  
  123.     color(foreColor);
  124.  
  125.     x = l + LINEINSET;
  126.     for (lt = 0; lt < NLINES; ++lt)
  127.     {
  128.     rectf(x, b + LINEINSET, x + lt, t - LINEINSET);
  129.     x += LINESPACE + lt;
  130.     }
  131.  
  132.     y = b + LINEINSET;
  133.     for (lt = 0; lt < NLINES; ++lt)
  134.     {
  135.     rectf(x, y, r - LINEINSET, y + lt);
  136.     y += LINESPACE + lt;
  137.     }
  138.     fx = x;
  139.     for (rt = 0; rt < NRECTS; ++rt)
  140.     {
  141.     rectf(x, y, x + rt, y + rt);
  142.     x += RECTSPACE + rt;
  143.     }
  144.     fy = y + rt + LINESPACE;
  145.     cmov2i(fx, fy);
  146.     fmprstr("Text");
  147. }
  148.  
  149. DoTest(int l, int r, int b, int t)
  150. /*Does a test pattern*/
  151. {
  152.     int backColor, foreColor, sl, sr, sb, st, i, j;
  153.  
  154.     for (i = 0; i < 8; ++i)
  155.     {
  156.     backColor = i;
  157.     foreColor = 0;
  158.     sl = l + i * (r - l) / 8;
  159.     sr = sl + (r - l) / 8;
  160.  
  161.     for (j = 0; j < 8; ++j)
  162.     {
  163.         sb = b + j * (t - b) / 8;
  164.         st = sb + (t - b) / 8;
  165.  
  166.         TestBlock(sl, sr, sb, st, foreColor, backColor);
  167.  
  168.         ++foreColor;
  169.     }
  170.     } 
  171. }
  172. #endif
  173.  
  174. int IsAssigned(c)
  175. int c;
  176. /*Returns true iff c is assigned*/
  177. {
  178.     int k;
  179.     for (k = 0; k < N_CHARS; ++k)
  180.     {
  181.     if (charAssignments[k] == c) return 1;
  182.     }
  183.     return 0;
  184. }
  185.  
  186. void DrawCharBox(c)
  187. int c;
  188. {
  189.     int i, j;
  190.     unsigned char s[2];
  191.  
  192.     i = c % 16;
  193.     j = c / 16 - 8;
  194.  
  195.     if (j < 0 || j >= 8) return;
  196.  
  197.     if (IsAssigned(c))
  198.     {
  199.     color(YELLOW);
  200.     }
  201.     else
  202.     {
  203.     color(WHITE);
  204.     }
  205.     rectfi(i * BLOCKSIZE + 1, (7 - j) * BLOCKSIZE + 2,
  206.         i * BLOCKSIZE + BLOCKSIZE - 2, (8 - j) * BLOCKSIZE - 2);
  207.  
  208.     color(BLACK);
  209.     s[0] = c;
  210.     s[1] = 0;
  211.     cmov2i(i * BLOCKSIZE + BLOCKSIZE / 2 - FONTSIZE / 2,
  212.            (7 - j) * BLOCKSIZE + BLOCKSIZE / 2 - FONTSIZE / 2);
  213. #ifdef FONTS4D
  214.     fmprstr((const char *) s);
  215. #else
  216.     charstr(s);
  217. #endif
  218. }
  219.  
  220. void DrawFontBoxes()
  221. /*Draws all the font boxes*/
  222. {
  223.     int i, j;
  224. #ifdef FONTS4D
  225.     fmfonthandle font;
  226. #endif
  227.  
  228.     linewidth(LINEWIDTH);
  229.  
  230.     /*Draw black lines*/
  231.     color(BLACK);
  232.     for (j = 0; j < 8; ++j)
  233.     {
  234.     move2i(0, BLOCKSIZE * 8 - j * BLOCKSIZE);
  235.     draw2i(BLOCKSIZE * 16, BLOCKSIZE * 8 - j * BLOCKSIZE);
  236.     }
  237.     for (i = 1; i < 16; ++i)
  238.     {
  239.     move2i(i * BLOCKSIZE, BLOCKSIZE * 8);
  240.     draw2i(i * BLOCKSIZE, 0);
  241.     }
  242.     move2i(0, BLOCKSIZE * 9);
  243.     draw2i(BLOCKSIZE * NOTFOUNDSIZE, BLOCKSIZE * 9);
  244.     move2i(BLOCKSIZE * NOTFOUNDSIZE, BLOCKSIZE * 9);
  245.     draw2i(BLOCKSIZE * NOTFOUNDSIZE, BLOCKSIZE * 8);
  246.  
  247.     /*Draw the fonts*/
  248. #ifdef FONTS4D
  249.     font = fmfindfont("Helvetica");
  250.     if (font)
  251.     {
  252.     font = fmscalefont(font, FONTSIZE);
  253.     fmsetfont(font);
  254.     }
  255.     else
  256.     {
  257.     printf("The font Helvetica cannot be found on your system.  There must be \n\
  258. something wrong with your system.  Please check the font system and try\n\
  259. again.  If you cannot fix the problem, you can still install SciAn without\n\
  260. the Silicon Graphics font manager by removing the #define FONTS4D from the\n\
  261. machin.h include file.\n");
  262.     exit(-1);
  263.     }
  264. #else
  265.     font(0);
  266. #endif
  267.  
  268.     for (j = 0; j < 8; ++j)
  269.     {
  270.     for (i = 0; i < 16; ++i)
  271.     {
  272.         DrawCharBox(128 + i + j * 16);
  273.     }
  274.     }
  275.     cmov2i(BLOCKSIZE / 2 - FONTSIZE / 2, 8 * BLOCKSIZE + BLOCKSIZE / 2 - FONTSIZE / 2);
  276. #ifdef FONTS4D
  277.     fmprstr("Not Shown");
  278. #else
  279.     charstr("Not Shown");
  280. #endif
  281. }
  282.  
  283. void PrintString(x, y, s)
  284. int x, y;
  285. char *s;
  286. /*Prints string at x, y with carriage return*/
  287. {
  288.     char *s2;
  289.     while (*s)
  290.     {
  291.     s2 = tempStr;
  292.     while(*s && (*s != '\n'))
  293.     {
  294.         *s2++ = *s++;
  295.     }
  296.     *s2 = 0;
  297.     if (*s) ++s;
  298.     cmov2i(x, y);
  299.     charstr(tempStr);
  300.     y -= INSTRUCTIONLINE;
  301.     }
  302. }
  303.  
  304. void DrawInstructions()
  305. /*Draws the instructions*/
  306. {
  307.     int height;
  308.     color(WHITE);
  309.     rectfi(0, 9 * BLOCKSIZE + 1, 16 * BLOCKSIZE, 9 * BLOCKSIZE + TOPSPACE);
  310.     color(BLACK);
  311.     PrintString(INSTRUCTIONX, INSTRUCTIONY, instructions[state]);
  312.  
  313.     switch(state)
  314.     {
  315.     case ST_COPYRIGHT:
  316.         /*Draw the copyright symbol*/
  317.         circi(SAMPLEX, SAMPLEY, SAMPLERADIUS);
  318.         arci(SAMPLEX, SAMPLEY, SAMPLERADIUS * 0.6, 450, 3150);
  319.         break;
  320.     case ST_REGISTERED:
  321.         /*Draw the registered copyright symbol*/
  322.         circi(SAMPLEX, SAMPLEY, SAMPLERADIUS);
  323.         height = SAMPLERADIUS * 0.6;
  324.         move2i(SAMPLEX, SAMPLEY + height);
  325.         draw2i(SAMPLEX - height / 2, SAMPLEY + height);
  326.         draw2i(SAMPLEX - height / 2, SAMPLEY - height);
  327.         move2i(SAMPLEX - height / 2, SAMPLEY);
  328.         draw2i(SAMPLEX, SAMPLEY);
  329.         draw2i(SAMPLEX + height / 2, SAMPLEY - height);
  330.         arci(SAMPLEX, SAMPLEY + height / 2, height / 2, -900, 900);
  331.         break;
  332.     case ST_CROSS:
  333.         /*Draw the cross symbol*/
  334.         move2i(SAMPLEX - SAMPLERADIUS, SAMPLEY - SAMPLERADIUS);
  335.         draw2i(SAMPLEX + SAMPLERADIUS, SAMPLEY + SAMPLERADIUS);
  336.         move2i(SAMPLEX - SAMPLERADIUS, SAMPLEY + SAMPLERADIUS);
  337.         draw2i(SAMPLEX + SAMPLERADIUS, SAMPLEY - SAMPLERADIUS);
  338.         break;
  339.     case ST_DOT:
  340.         /*Draw the dot symbol*/
  341.         cmov2i(SAMPLEX, SAMPLEY);
  342.         charstr(".");
  343.         break;
  344.     }
  345. }
  346.  
  347. void DrawEverything()
  348. /*Draws everything*/
  349. {
  350.     color(WHITE);
  351.     clear();
  352.     DrawFontBoxes();
  353.     DrawInstructions();
  354. }
  355.  
  356. void IncrementState()
  357. /*Increments the state by 1*/
  358. {
  359.     ++state;
  360.     if (state >= N_STATES)
  361.     {
  362.     running = 0;
  363.     }
  364.     else
  365.     {
  366.     DrawInstructions();
  367.     }
  368. }
  369.  
  370. void AssignChar(c, v)
  371. int c, v;
  372. /*Assigns character c to value v*/
  373. {
  374.     charAssignments[c] = v;
  375.     DrawCharBox(v);
  376. }
  377.  
  378. void UnassignChar(c)
  379. int c;
  380. /*Unassigns character c*/
  381. {
  382.     int v;
  383.     v = charAssignments[c];
  384.     charAssignments[c] = 0;
  385.     if (v)
  386.     {
  387.     DrawCharBox(v);
  388.     }
  389. }
  390.  
  391. void DecrementState()
  392. /*Decrements the state by 1*/
  393. {
  394.  
  395.     --state;
  396.     if (state < 0)
  397.     {
  398.     running = 0;
  399.     }
  400.     else
  401.     {
  402.     /*Do stuff for decrementing state*/
  403.     switch (state)
  404.     {
  405.         case ST_COPYRIGHT:
  406.         UnassignChar(CHAR_COPYRIGHT);
  407.         break;
  408.         case ST_REGISTERED:
  409.         UnassignChar(CHAR_REGISTERED);
  410.         break;
  411.         case ST_CROSS:
  412.         UnassignChar(CHAR_CROSS);
  413.         break;
  414.         case ST_DOT:
  415.         UnassignChar(CHAR_DOT);
  416.         break;
  417.     }
  418.     DrawInstructions();
  419.     }
  420. }
  421.  
  422. int GetCharFromPress(x, y)
  423. long x, y;
  424. /*Gets a character from a press, or 0 if not found*/
  425. {
  426.     int i, j;
  427.  
  428.     j = x / BLOCKSIZE;
  429.     i = 7 - (y / BLOCKSIZE);
  430.     if (i < 0) return 0;
  431.     return 128 + i * 16 + j;
  432. }
  433.  
  434. void AssignXYChar(c, x, y)
  435. int c, x, y;
  436. /*Assign a character c in response to a click in x, y*/
  437. {
  438.     int v;
  439.     v = GetCharFromPress(x, y);
  440.     AssignChar(c, v);
  441. }
  442.  
  443. void MousePress(x, y)
  444. long x, y;
  445. /*Press of the mouse*/
  446. {
  447.     int c;
  448.  
  449.     switch (state)
  450.     {
  451.     case ST_COPYRIGHT:
  452.         /*Assign the copyright character*/
  453.         AssignXYChar(CHAR_COPYRIGHT, x, y);
  454.         break;
  455.     case ST_REGISTERED:
  456.         /*Assign the registered character*/
  457.         AssignXYChar(CHAR_REGISTERED, x, y);
  458.         break;
  459.     case ST_CROSS:
  460.         /*Assign the cross character*/
  461.         AssignXYChar(CHAR_CROSS, x, y);
  462.         break;
  463.     case ST_DOT:
  464.         /*Assign the dot character*/
  465.         AssignXYChar(CHAR_DOT, x, y);
  466.         break;
  467.     default:
  468.         break; 
  469.     }
  470. }
  471.  
  472. main()
  473. {
  474.     int k;
  475.     int width, height;
  476.     long ox, oy;
  477.     long mx, my;
  478.     long win;
  479.  
  480.     for (k = 0; k < N_CHARS; ++k)
  481.     {
  482.     charAssignments[k] = 0;
  483.     }
  484.  
  485. #ifdef FONTS4D
  486.  
  487.     foreground();
  488.  
  489.     width = BLOCKSIZE * 16;
  490.     height = BLOCKSIZE * 9 + TOPSPACE;
  491.  
  492.     minsize(width, height);
  493.     maxsize(width, height);
  494.  
  495.     win = winopen("Font test");
  496.     winset(win);
  497.     wintitle("Font test");
  498.     ortho2(-0.5,width - 0.5,-0.5,height - 0.5);
  499.  
  500.     fminit();
  501.  
  502.     DrawEverything();
  503.  
  504.     qdevice(MOUSE1);
  505.     qdevice(MOUSE2);
  506.     qdevice(MOUSE3);
  507.     qdevice(WINQUIT);
  508.     qdevice(WINCLOSE);
  509.     qdevice(ESCKEY);
  510.  
  511.     while(running)
  512.     {
  513.     while (qtest())
  514.      {
  515.         long whichDevice;
  516.         short theData;
  517.  
  518.         whichDevice = qread(&theData);
  519.         switch(whichDevice)
  520.         {
  521.         case REDRAW:
  522.         case PIECECHANGE:
  523.             DrawEverything();
  524.             break;
  525.         case WINQUIT:
  526.         case WINCLOSE:
  527.             running = 0;
  528.             break;
  529.         case MOUSE1:
  530.         case MOUSE2:
  531.         case MOUSE3:
  532.             if (theData)
  533.             {
  534.             mx = getvaluator(MOUSEX);
  535.             my = getvaluator(MOUSEY);
  536.             getorigin(&ox, &oy);
  537.             mx -= ox;
  538.             my -= oy;
  539.             MousePress(mx, my);
  540.             }
  541.             else
  542.             {
  543.             IncrementState();
  544.             }
  545.             break;
  546.         case ESCKEY:
  547.             if (theData)
  548.             {
  549.             DecrementState();
  550.             }
  551.             break;
  552.         }
  553.     }
  554.     }
  555.  
  556.     unqdevice(ESCKEY);
  557.     unqdevice(WINCLOSE);
  558.     unqdevice(WINQUIT);
  559.     unqdevice(MOUSE1);
  560.     unqdevice(MOUSE2);
  561.     unqdevice(MOUSE3);
  562.  
  563. #else
  564.     printf("Your workstation does not have the SGI font manager installed, so the \n");
  565.     printf("default system font and defined raster fonts will be used.  The fonts\n");
  566.     printf("should work correctly.\n");
  567. #endif
  568.  
  569.     if (state > 0)
  570.     {
  571.     /*Write the file*/
  572.     FILE *outFile;
  573.     outFile = fopen("ScianFontMappings.h", "w");
  574.     if (outFile)
  575.     {
  576.         fprintf(outFile, "/*ScianFontMappings.h is produced by the ScianFontMapper.c program\n\
  577.   during the make FONTS process.  Do not change this file.\n*/\n\n");
  578.         for (k = 0; k < N_CHARS; ++k)
  579.         {
  580.         if (charAssignments[k])
  581.         {
  582.             fprintf(outFile, "#define ST_%s \"\\%o\"\n",
  583.             charNames[k],
  584.             charAssignments[k]);
  585.             fprintf(outFile, "#define CH_%s \'\\%o\'\n",
  586.             charNames[k],
  587.             charAssignments[k]);
  588.         }
  589.         else
  590.         {
  591.             fprintf(outFile, "#define ST_%s\n",
  592.             charNames[k]);
  593.         }
  594.         }
  595.         fclose(outFile);
  596.     }
  597.     else
  598.     {
  599.         printf("The output file ScianFontMappings.h cannot be created.  Check to make\n\
  600. sure that you have write access to this directory, the disk is not full, and\n\
  601. there is currently no file with that name that you cannot overwrite and try\n\
  602. again.");
  603.     }
  604.     }
  605. }
  606.